package barinadroid.game;

import java.util.ArrayList;
import org.andengine.entity.Entity;

public class BulletFactory
{
	public static BulletFactory instance;

	public static BulletFactory getSharedInstance()
	{
		if(instance == null)
			instance = new BulletFactory();
		return instance;
	}

	private ArrayList<Bullet> bullets;
	private BulletPool pool;

	private BulletFactory()
	{
		bullets = new ArrayList<Bullet>();
		pool = BulletPool.sharedBulletPool();
	}

	public synchronized Bullet Shoot(GameEntity shooter)
	{
		Bullet bullet = pool.obtainPoolItem().ShotBy(shooter);
		bullets.add(bullet);
		return bullet;
	}

	public ArrayList<Bullet> getBullets()
	{
		return this.bullets;
	}

	public ArrayList<Bullet> getBullets(Entity shooter)
	{
		ArrayList<Bullet> result = new ArrayList<Bullet>();
		for(Bullet bullet : this.bullets)
		{
			if(bullet.getOwner() == shooter)
				result.add(bullet);
		}
		return this.bullets;
	}

	public synchronized void DestroyBullet(Bullet bullet)
	{
		bullet.setActive(false);
		bullet.detachSelf();
		pool.recyclePoolItem(bullet);
		bullets.remove(bullet);
	}

	public synchronized void DestroyAllBullets()
	{
		for(Bullet bullet : bullets)
		{
			bullet.setActive(false);
			bullet.detachSelf();
			pool.recyclePoolItem(bullet);
		}
		bullets.clear();
	}
}